package onlineclass.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import onlineclass.entity.OResource;
import onlineclass.service.OResourceService;
import onlineclass.util.AjaxResult;
import onlineclass.util.StringUtil;
import onlineclass.util.AjaxPage;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class OResourceController {
    public void search(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        int pageNo = StringUtil.toInt(request.getParameter("pageNo"),1);
        String title = request.getParameter("title");
        AjaxPage types = OResourceService.INST.search(pageNo,10,title);
        response.setContentType("application/json;charset=utf-8");
        String str = JSON.toJSONStringWithDateFormat(types,"yyyy-MM-dd",
                SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty,
                SerializerFeature.WriteNullListAsEmpty,SerializerFeature.WriteNonStringValueAsString);
        response.getWriter().print(str);
    }
    public AjaxResult add(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        String title = request.getParameter("title");
        String url = request.getParameter("url");
        String icon = request.getParameter("icon");
        int inuse = StringUtil.toInt(request.getParameter("inuse"));
        OResource c = new OResource();
        c.setTitle(title);
        c.setUrl(url);
        c.setIcon(icon);
        c.setInuse(inuse);
        int num = OResourceService.INST.add(c);
        if (num>0){
            return AjaxResult.success("保存成功");
        }
//        String s = JSON.toJSONString(num);
//        response.getWriter().print(s);
        return AjaxResult.error("保存失败");
    }
//    接收修改对象的id
    public void get(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        int id = StringUtil.toInt(request.getParameter("id"));
        OResource get = OResourceService.INST.get(id);

        String str = JSON.toJSONString(get);
        response.getWriter().print(str);
    }

    public AjaxResult update(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        int id = StringUtil.toInt(request.getParameter("id"));
        String tiele = request.getParameter("tiele");
        String url = request.getParameter("url");
        String icon = request.getParameter("icon");
        int inuse = StringUtil.toInt(request.getParameter("inuse"));
        OResource c = new OResource();
        c.setTitle(tiele);
        c.setUrl(url);
        c.setIcon(icon);
        c.setInuse(inuse);
        c.setId(id);
        int num = OResourceService.INST.update(c);
        if (num>0){
            return AjaxResult.success("修改成功");
        }
        return AjaxResult.error("修改失败");
    }
    public AjaxResult del(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        OResource c = new OResource();
        int id = StringUtil.toInt(request.getParameter("id"));
        c.setInuse(0);
        c.setId(id);
        int num = OResourceService.INST.update(c);
        if (num>0){
            return AjaxResult.success("修改成功");
        }
        return AjaxResult.error("修改失败");
    }
}
